<template>
  <div>
    <!-- table区域-begin -->
    <div>

      <a-table
        ref='table'
        size='middle'
        :scroll='{x:true}'
        bordered
        rowKey='id'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        class='j-table-force-nowrap'
        @change='handleTableChange'>

        <template slot='htmlSlot' slot-scope='text'>
          <div v-html='text'></div>
        </template>
        <template slot='imgSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无图片</span>
          <img v-else :src='getImgView(text)' height='25px' alt=''
               style='max-width:80px;font-size: 12px;font-style: italic;' />
        </template>
        <template slot='fileSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无文件</span>
          <a-button
            v-else
            :ghost='true'
            type='primary'
            icon='download'
            size='small'
            @click='downloadFile(text)'>
            下载
          </a-button>
        </template>

        <div slot='action' slot-scope='text, record' v-if='record.contractBpmStatus ==="3"'>
          <!--销售/项目负责人，申请发票-->
          <div v-has="'ContractSalePaymentCollection:edit'">
            <a v-if="record.bpmStatus === '1'" @click='saleApplyInvoiceHandleDetail(record, false)'>申请</a>
            <template v-if="record.auditDataId">
              <a-divider type='vertical' v-if="record.auditDataId && record.bpmStatus === '1'"/>
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="saleApplyInvoiceHandleDetail(record, true)">详情</a>
                  </a-menu-item>
                    <a-menu-item v-if="record.bpmStatus === '1'">
                    <a @click="startApplyInvoiceProcess(record)">发起流程</a>
                  </a-menu-item>
                  <a-menu-item v-if="record.bpmStatus !== '1'" @click='handleTrack(record)'>审批进度</a-menu-item>
                </a-menu>
              </a-dropdown>
            </template>
          </div>
          <!-- 财务，回款确认 -->
          <span v-has='"ContractSalePaymentCollection:sure"'>
            <a v-if="record.auditDataId" @click="saleApplyInvoiceHandleDetail(record, true)">详情</a>
            <a-divider type='vertical'  v-if="record.auditDataId"/>
            <a @click='handleEdit(record)'>确认</a>
          </span>
        </div>

      </a-table>
    </div>

    <contract-sale-payment-collection-modal ref='modalForm' @ok='modalFormOk'></contract-sale-payment-collection-modal>
    <contract-sale-apply-invoice-modal ref='saleApplyInvoiceModalForm' @ok='saleApplyInvoiceOk'></contract-sale-apply-invoice-modal>
    <bpm-process-track-modal ref='bpmProcessTrackModal'></bpm-process-track-modal>
    <bpm-biz-task-deal-modal ref="taskDealModal" :path="path" :formData="formData" @ok="handleOk"></bpm-biz-task-deal-modal>
  </div>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import ContractSalePaymentCollectionModal from './modules/ContractSalePaymentCollectionModal'
import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import ContractSaleApplyInvoiceModal from '@views/hifar/contract/modules/ContractSaleApplyInvoiceModal'
import { getAction, postAction } from '@api/manage'
import { JeecgBpmListMixin } from '@/mixins/JeecgBpmListMixin'
import BpmBizTaskDealModal from '@views/modules/bpmbiz/common/BpmBizTaskDealModal'
import BpmProcessTrackModal from '@views/modules/bpmbiz/common/BpmProcessTrackModal'
import { toFixedTwo } from '@/utils/util'

export default {
  name: 'ContractSalePaymentCollectionList',
  mixins: [JeecgBpmListMixin, mixinDevice],
  components: {
    ContractSalePaymentCollectionModal,
    ContractSaleApplyInvoiceModal,
    BpmBizTaskDealModal,
    BpmProcessTrackModal,
  },
  props:{
    mainId:{
      type:String,
      default:'',
      required:false
    },
    // bpmStatus:{
    //   type:String,
    //   default:'',
    //   required:false
    // }
  },
  data() {
    return {
      description: '销售合同-回款管理管理页面',
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '回款条件',
          align: 'center',
          dataIndex: 'returnCondition_dictText',
        },
        {
          title: '计划回款金额',
          align: 'center',
          dataIndex: 'expectMoney'
        },
        {
          title: '计划回款时间',
          align: 'center',
          dataIndex: 'expectTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '回款负责人',
          align: 'center',
          dataIndex: 'paymantCharger'
        },
        {
          title: '实际回款金额',
          align: 'center',
          dataIndex: 'actualMoney'
        },
        {
          title: '实际回款时间',
          align: 'center',
          dataIndex: 'actualTime',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '是否开票',
          align: 'center',
          dataIndex: 'billing_dictText'
        },
        {
          title: '开票比例',
          align: 'center',
          dataIndex: 'billingRatio'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/contract/contractSalePaymentCollection/list',
        delete: '/contract/contractSalePaymentCollection/delete',
        deleteBatch: '/contract/contractSalePaymentCollection/deleteBatch',
        exportXlsUrl: '/contract/contractSalePaymentCollection/exportXls',
        importExcelUrl: 'contract/contractSalePaymentCollection/importExcel',
        queryById: 'contract/contractSalePaymentCollection/queryById',
        findByPaymentId: '/contract/contractSaleApplyInvoice/findByPaymentId'
      },
      flowCode: 'dev_hf_contract_sale_apply_invoice_001',
      dictOptions: {},
      superFieldList: [],
      queryModel: true
    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    refresh(row){
      this.queryParam.contractId = row.id
      this.loadData(1)
    },
    saleApplyInvoiceHandleDetail(record, disableSubmit) {
      let saleApplyInvoice = {}
      let that = this;
      getAction(this.url.findByPaymentId, {id: record.id}).then((res) => {
        if (res.success) {
          saleApplyInvoice = res.result;
          that.$refs.saleApplyInvoiceModalForm.edit(saleApplyInvoice)
          that.$refs.saleApplyInvoiceModalForm.title = '销售合同开票信息'
          that.$refs.saleApplyInvoiceModalForm.disableSubmit = disableSubmit
        }else {
          this.$message.warning(res.message);
        }
      })

    },
    loadData(arg) {
      if(!this.url.list){
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.dataSource.forEach((v)=>{
            v.expectMoney = toFixedTwo(v.expectMoney) || 0
            v.actualMoney = toFixedTwo(v.actualMoney) || 0
          })
          this.ipagination.total = res.result.total;
        }
        this.loading = false;
      })
    },
    handleEdit(record) {
      let that = this;
      getAction(this.url.queryById, {id: record.id}).then((res) => {
        if (res.success) {
          this.$refs.modalForm.edit(res.result);
          this.$refs.modalForm.title = "回款确认";
          this.$refs.modalForm.disableSubmit = false;
        }else {
          this.$message.warning(res.message);
        }
      })
    },

    startApplyInvoiceProcess(record){
      this.$confirm({
        title:'提示',
        content:'确认提交流程吗?',
        onOk:()=>{
          let params = {
            flowCode: this.flowCode,
            id: record.auditDataId,
            formUrl: 'hifar/contract/modules/ContractSaleApplyInvoiceForm',
            formUrlMobile: ''
          }
          postAction(this.url.startProcess, params).then(res=>{
            if(res.success){
              this.$message.success(res.message);
              this.loadData();
              this.onClearSelected();
            }else{
              this.$message.warning(res.message);
            }
          }).catch((e)=>{
            this.$message.warning('不识别的请求!');
          })
        }
      })
    },

    handleTrack(record){
      var params = {flowCode:this.flowCode,dataId:record.auditDataId};//查询条件
      this.$refs.bpmProcessTrackModal.handleTrack(params);
      this.$refs.bpmProcessTrackModal.title="审批跟踪记录";
    },

    saleApplyInvoiceOk() {
      this.loadData(1)
    }
  },
  watch: {
    // mainId: {
    //   immediate: true,
    //   handler(val) {
    //     if (!this.mainId) {
    //      // this.clearList()
    //     } else {
    //       this.queryParam['contractId'] = val
    //       this.loadData(1)
    //     }
    //   }
    // }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>